import React, { Component } from 'react';
import {
    Platform,
    StyleSheet,
    Text,
    View,
    Button,
    TextInput,
    FlatList,
    RefreshControl,
    ActivityIndicator,
    Alert,
    AsyncStorage,
    WebView
} from 'react-native';
import NavigationBar from '../common/NavigationBar';
import ViewUtils from '../common/util/ViewUtils';
const URL = 'https://github.com'
export default class WebviewTest extends Component {
    constructor(props){
        super(props)
        this.url = (this.props.navigation.state.params.dataFather.item && this.props.navigation.state.params.dataFather.item.item) ? this.props.navigation.state.params.dataFather.item.item.html_url : `${URL}/${this.props.navigation.state.params.dataFather.fullName}`;
        let title = (this.props.navigation.state.params.dataFather.item && this.props.navigation.state.params.dataFather.item.item) ? this.props.navigation.state.params.dataFather.item.item.full_name : this.props.navigation.state.params.dataFather.fullName;
        this.state = {
            url: this.url,
            title: title,
            canGoBack: false
        }
    }
    componentWillMount(){
        console.log("props", this.props)
    }
    onBack(){
        if(this.state.canGoBack) {
            this.webView.goBack();
        } else {
            this.props.navigation.pop();
        }
    }
    go(){
        this.setState({
            url: this.text
        })
    }
    onNavigationStateChange(e){
        this.setState({
            canGoBack: e.canGoBack,
        })
    }
    render(){
        return (
            <View style={styles.container}>
                <NavigationBar
                    statusBar={{
                        backgroundColor:'#6495ED'
                    }}
                    title={this.state.title}
                    style = {{
                        backgroundColor: '#6495ED'
                    }}
                    leftButton={ViewUtils.getLeftButton(()=>{
                        this.onBack();
                    })}
                />
                {/*<View style={styles.row}>*/}
                    {/*<TextInput*/}
                        {/*style= {styles.input}*/}
                        {/*defaultValue={URL}*/}
                        {/*onChangeText={(text)=>{*/}
                            {/*this.text = text;*/}
                        {/*}}*/}
                    {/*/>*/}
                    {/*<Text*/}
                        {/*onPress={()=>{*/}
                            {/*this.go();*/}
                        {/*}}*/}
                    {/*>前往</Text>*/}
                {/*</View>*/}
                <WebView
                    ref= {webView=>this.webView=webView}
                    source={{uri: this.state.url}}
                    onNavigationStateChange={(e)=>{
                        this.onNavigationStateChange(e);
                    }}
                    startInLoadingState={true}
                />
            </View>
        )
    }
}

styles = StyleSheet.create({
    container: {
        flex: 1
    },
    row: {
        flexDirection: 'row',
        alignItems: 'center',
        margin: 10
    },
    input: {
        height: 40,
        flex: 1,
        borderWidth: 1,
        margin: 2
    }
})